package com.benben;

import java.math.*;
import java.security.InvalidParameterException;
import java.util.Scanner;

public class BaseKnowledge {
	/**
	 * 计算一个数的阶乘A<sub>n</sub> = n! = n*(n-1)*.....*2*1
	 * 
	 * @param n
	 * @return n的阶乘
	 */
	public static int factorial(int n) {
		if (n < 0) {
			throw new InvalidParameterException("非法的参数:" + n);
		}
		if (n == 0 || n == 1) {
			return 1;
		}
		return n * factorial(n - 1);
	}

	/**
	 * m个元素取n个进行无序排列 计算排列组合C<sub>m</sub><sup>n</sup> = m ! / n!(m - n)!
	 * 
	 * @param m
	 *            总元素个数
	 * @param n
	 *            进行排列的元素个数
	 */
	public static int combination(int m, int n) {
		if (n > m) {
			throw new InvalidParameterException("非法");
		}
		if (n < 0 || m < 0) {
			throw new InvalidParameterException("非法的参数:" + n);
		}
		int factor = factorial(m);
		int factor1 = factorial(n);
		int factor2 = factorial(m - n);
		return factor / (factor1 * factor2);
	}

	public static String strCon() {
		Scanner s = new Scanner(System.in);
		System.out.print("请输入一个字符串：");
		String str1 = s.nextLine();
		System.out.print("请再输入一个字符串：");
		String str2 = s.nextLine();
		String str = str1 + str2;
		System.out.println("连接后的字符串是：" + str);
		return str;
	}

	/**
	 * 题目：读取7个数（1—50）的整数值，每读取一个值，程序打印出该值个数的*
	 */
	public static void Sample1() {
		int tCount = 7;
		int aCount;
		int i;
		int flag = 0;
		Scanner s = new Scanner(System.in);

		for (i = 1; i <= tCount; i++) {
			// System.out.println("");

			System.out.print("请输入第" + i + "个整数：");
			aCount = s.nextInt();
			if (aCount < 1 || aCount > 50)
				System.out.println("请输入一个1~50之间的数据");
			else {
				for (int j = 1; j <= aCount; j++) {
					System.out.print("*");
				}
				System.out.println(" ");

			}
		}
		if (i > 7) {
			System.out.println("最多可以输入7个数");
		}
	}

	/**
	 * 题目：编写一个函数，输入n为偶数时，
	 * 调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)
	 */
	public static void OddEvenCal() {

		Scanner s = new Scanner(System.in);
		System.out.println("Please input one count:");
		int count = s.nextInt();
		System.out.println("count:" + count);
		System.out.println("count cal:" + count % 2);
		if (count % 2 == 0) {
			System.out.println("Deal with evenCount:");
			System.out.println("Output even sum result:"
					+ BaseKnowledge.EvenCal(count));
		} else {
			System.out.println("Deal with oddCount:");
			System.out.println("Output odd sum result :"
					+ BaseKnowledge.OddCal(count));
		}
	}

	public static double EvenCal(int evenCount) {

		double sum = 0;
		int n = evenCount / 2;
		float cal;
		System.out.println("even count n :" + n);
		for (int i = 1; i <= n; i++) {
			// sum = sum + 1/ (i* 2 );
			cal = i * 2;
			sum = sum + 1 / cal;
			System.out.println("sum is :" + sum);
		}
		return sum;
	}

	public static double OddCal(int oddCount) {
		double sum = 0;
		int m = (int) Math.floor(oddCount / 2) + 1;
		float cal;
		System.out.println("odd count m :" + m);
		for (int j = 1; j <= m; j++) {
			cal = 2 * j - 1;
			sum = sum + 1 / cal;
		}

		return sum;
	}

	/**
	 * 题目：写一个函数，求一个字符串的长度，在main函数中输入字符串，并输出其长度。
	 */
	public static void StrLength() {

		/**
		 * 请输入一个字符串
		 */
		Scanner sr = new Scanner(System.in);
		String recStr = sr.nextLine();
		System.out.println("输入的字符串为：" + recStr);
		int charCount = recStr.length();
		System.out.println("字符串的长度为：" + charCount);

	}

	/**
	 * 题目：有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数
	 */
	public static void RemoveLoc() {
		/**
		 * 初始化数组
		 */
		Scanner s = new Scanner(System.in);
		System.out.println("初始化数组的数字个数:");
		int InitCount = s.nextInt();
		// System.out.println("初始化数组的数字个数为：" + InitCount);
		System.out.println("后移的位置为:");
		int RemCount = s.nextInt();
		// System.out.println("后移的位置为：" + RemCount);
		int var = InitCount - RemCount;
		int InitArray[] = new int[InitCount];
		int ResArray[] = new int[InitCount];
		for (int i = 1; i <= InitCount; i++) {
			InitArray[i - 1] = i;
		}
		/**
		 * 开始赋值
		 */
		for (int j = 0; j < InitCount; j++) {
			if (j < RemCount) {
				ResArray[j] = InitArray[j + var];
			} else {
				ResArray[j] = InitArray[j - RemCount];
			}

		}
		/**
		 * 输出结果数组，验证结果
		 */
		System.out.println("Output the resutl Array:");
		for (int m = 0; m < ResArray.length; m++) {
			System.out.print(ResArray[m] + ",");
		}
	}

	/**
	 * 题目：输入数组，最大的与第一个元素交换，最小的与最后一个元素交换，输出数组。
	 */
	public static void ExcElement() {
		/**
		 * 初始化數組
		 */
		Scanner s = new Scanner(System.in);
		System.out.println("請輸入初始化數組的元素個數：");
		int InitCount = s.nextInt();
		int initArray[] = new int[InitCount];
		for (int i = 1; i <= InitCount; i++) {
			initArray[i - 1] = i + 10;
		}
		System.out.println("最小的數組元素為：" + BaseKnowledge.MinElement(initArray));
	}

	public static int MinElement(int initArray[]) {

		int minEle = initArray[0];

		for (int i = 1; i < initArray.length; i++) {
			if (initArray[i] < minEle) {
				minEle = initArray[i];
			}
		}

		return minEle;
	}

	/**
	 * Write a program called calculateSum to produce the sum of 1, 2, 3, ...,
	 * to an upperbound (e.g., 100).And display the result.
	 */
	public static void CalSum() {
		Scanner s = new Scanner(System.in);
		System.out.println("請輸入求和計算的上限數字：");
		int uppBound = s.nextInt();
		int sum = 0;
		for (int i = 1; i <= uppBound; i++) {
			sum = sum + i;
		}
		System.out.println("數據之和為：" + sum);
	}

	/**
	 * Modify the program to find the "sum of the squares" of all the numbers
	 * from 1 to 100, i.e. 1*1 + 2*2 + 3*3 + ... + 100*100
	 */
	public static void SquSum() {
		Scanner s = new Scanner(System.in);
		System.out.println("請輸入求平方計算的上限數字：");
		int uppBound = s.nextInt();
		int sum = 0;
		for (int i = 1; i <= uppBound; i++) {
			sum = sum + i * i;
		}
		System.out.println("數據之和為：" + sum);
	}

	/**
	 * Write a program called TimeTable to produce the multiplication table of 1
	 * to 9 as shown using two nested for-loops:
	 */
	public static void TimeTable() {
		System.out.println(" * |  1  2  3  4  5  6  7  8  9");
		System.out.println("-------------------------------");
		System.out.println("                               ");
		for (int i = 1; i <= 9; i++) {
			System.out.print(" " + i + " |");
			for (int j = 1; j <= 9; j++) {
				System.out.print("  " + i * j);

			}
			System.out.println();
		}
	}

	/**
	 * Write a program called MulTable to produce the multiplication table of 1
	 * to 9 as shown using two nested for-loops:
	 */
	public static void MulTable() {
		System.out.println(" * |  1  2  3  4  5  6  7  8  9");
		System.out.println("-------------------------------");
		System.out.println("                               ");
		for (int i = 1; i <= 9; i++) {
			System.out.print(" " + i + " |");
			for (int j = 1; j <= i; j++) {
				System.out.print("  " + i * j);

			}
			System.out.println();
		}
	}

	/**
	 * 写一个函数 求1/1 + 1/2+ 1/3 + ......+ 1/n的和
	 */
	public static void FracSum() {
		Scanner s = new Scanner(System.in);
		System.out.println("請輸入倒數方計算的上限數字：");
		int topLimit = s.nextInt();
		double resSum = (double) 0;
		double midRes = (double) 0;
		for (int i = 1; i <= topLimit; i++) {
			midRes = (double) 1 / i;

			resSum = resSum + midRes;

		}
		System.out.println("倒數求和為：" + resSum);
	}

	/**
	 * 题目：取一个整数a从右端开始的4～7位。
	 */
	public static void FetchInt() {
		// get a Int
		int max_check_times = 3;
		Scanner s = new Scanner(System.in);
        String getString ;
		int getInt =0 ;
		String subStr ;

		for (int i = 1; i <= max_check_times; i++) {
			System.out.println("請輸入一個7位以上的數字：");
			getInt = s.nextInt();
			if (getInt >= 1000000) {
				System.out.println("數據" + getInt + "校驗通過");
				break;
			} else {
				if (i < 3) {
					System.out.println("數據" + getInt + "不合法，請再次輸入:");
				} else {
					System.out.println("數據" + getInt + "您已經錯誤輸入3次,請退出后,重新登陸");
					System.exit(0);
				}
			}

		}
		//
		//getString =  toString(getInt);
	    getString = String.valueOf(getInt);

		System.out.println("getString :" + getString );
		
		subStr = getString.substring (getString.length()-7 ,getString.length()-3);
		System.out.println("subString:" + subStr);
		System.out.println("數字" + getInt + "的4～7位是：" + subStr);
	}
	
	private static final int MAX_CHECK_TTIMES = 3;

	public static void fetchInt() {
		int value = 0;
		System.out.println("請輸入一個7位以上的數字：");
		try {
			for (int i = 0; i < MAX_CHECK_TTIMES; i++) {
				Scanner s = new Scanner(System.in);
				value = s.nextInt();
				boolean valid = check7Numbers(value);
				if (!valid) {
					System.out.println("數據不合法，請再次輸入:");
					continue;
				} else {
					String valueStr = String.valueOf(value);
					int len = valueStr.length();
					valueStr = valueStr.substring(len - 7, len - 3);
					System.out.println("數字" + value + "的4～7位是：" + valueStr);
					break;
				}
			}
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println("输入异常!程序终止!");
		}
	}
	
	/**
	 * 判断整数是否是合法的
	 * @param value
	 * @return
	 */
	private static boolean check7Numbers(int value) {
//		if (value >= 1000000) {
//			return true;
//		}
		//或者
		if (String.valueOf(value).length() >= 7) {
			return true;
		}
		return false;
	}
	/**
	 * 輸入一個整數
	 * @return integer
	 */
	public static int inputInt(){
		Scanner s = new Scanner(System.in);
		try{
		  int outInt = s.nextInt();
		  return outInt;
		}catch(Exception e){
			System.out.println( "输入异常!程序终止,返回");
			return  -1;
		}
		
			
	}
	/**
	 * 题目：输入3个数a,b,c，按大小顺序输出
	 */
	public  static void sortInt(){
		int a = 0;
		int b = 0;
		int c = 0;
		int midvar ; 
		int mid[] = new int[3];
		int res[] = new int[3];
 		Scanner s = new Scanner(System.in);
		try{
		System.out.println("請輸入第一個數字：");
		
		a = s.nextInt();
		
	    System.out.println("a:" + a);
	    
		System.out.println("請輸入第二個數字：");
		
		b = s.nextInt();
		System.out.println("請輸入第三個數字");
		 
		c = s.nextInt();
		System.out.println("a:" + a + ";b:" + b + ";c:" + c);
		}catch(Exception e){
			System.out.println( "输入异常!程序终止");
		}
		mid[0] = a; 
		mid[1] = b;
	    mid[2] = c;
	    
		
	}
	
}
